# -*- coding: utf-8 -*-
import openpyxl

from runtimeError import runtimeError


def generate_excel_by_data(table_head, table_data, table_name):
    try:
        wb = openpyxl.Workbook()
        sheet = wb.create_sheet(index=0, title="Sheet1")
        sheet.append(table_head)
        # 存放数据
        for data in table_data:
            sheet.append(data)
        # 保存数据
        if len(table_name) == 0:
            table_name = "导出表"
        file_name = table_name + ".xlsx"
        wb.save(filename=file_name)
    except Exception as e:
        raise runtimeError("导出Excel遇到问题。具体：" + e.args[0])
